home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / ncpfs / ipxripd-.000 / ipxripd- / ipxripd / ipxkern.c < prev    next >
C/C++ Source or Header  |  1996-02-01  |  5KB  |  227 lines

  1. /*
  2.     IPX support library - kernel dependent functions
  3.  
  4.     Copyright (C) 1994, 1995  Ales Dryak <e-mail: A.Dryak@sh.cvut.cz>
  5.     Copyright (C) 1996, Volker Lendecke <lendecke@namu01.gwdg.de>
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/socket.h>
  26. #include <string.h>
  27. #include <linux/route.h>
  28. #include <netinet/in.h>
  29. #include "ipxkern.h"
  30.  
  31. #define MAX_IFC (256)
  32.  
  33. static int 
  34. asc2frame(char *frame)
  35. {
  36.     if (strcasecmp("etherii", frame) == 0)
  37.         return IPX_FRAME_ETHERII;
  38.     else if (strcasecmp("802.2", frame) == 0)
  39.         return IPX_FRAME_8022;
  40.     else if (strcasecmp("802.3", frame) == 0)
  41.         return IPX_FRAME_8023;
  42.     else if (strcasecmp("snap", frame) == 0)
  43.         return IPX_FRAME_SNAP;
  44.     else
  45.         return 0;
  46. }
  47.  
  48. int
  49. ipx_kern_scan_rtable(IPXrtScanFunc f, void *data)
  50. {
  51.     FILE *ipx_route;
  52.     char buf[512];
  53.  
  54.     ipx_route = fopen("/proc/net/ipx_route", "r");
  55.  
  56.     if (ipx_route == NULL)
  57.     {
  58.         sprintf(ipx_err_string, "open ipx_route: %s",
  59.             strerror(errno));
  60.         return -1;
  61.     }
  62.  
  63.     /* ignore header line */
  64.     fgets(buf, sizeof(buf), ipx_route);
  65.  
  66.     while (fgets(buf, sizeof(buf), ipx_route) != NULL)
  67.     {
  68.         IPXNet network;
  69.         IPXNet router_net;
  70.         IPXNode router_node;
  71.         int type = 0;
  72.  
  73.         if (strncmp(&(buf[11]), "Directly", 8) == 0)
  74.         {
  75.             if (ipx_sscanf_net(buf, &network) != 0)
  76.             {
  77.                 fclose(ipx_route);
  78.                 return -1;
  79.             }
  80.             router_net = 0;
  81.             memset(router_node, 0, sizeof(router_node));
  82.         }
  83.         else
  84.         {
  85.             type |= IPX_KRT_ROUTE;
  86.  
  87.             if (   (ipx_sscanf_net(buf, &network) != 0)
  88.                 || (ipx_sscanf_net(&(buf[11]), &router_net) != 0)
  89.                 || (ipx_sscanf_node(&(buf[24]), router_node) != 0))
  90.             {
  91.                 fclose(ipx_route);
  92.                 return -1;
  93.             }
  94.         }
  95.  
  96.         if (f(network, router_net, router_node, type, data) != 0)
  97.         {
  98.             fclose(ipx_route);
  99.             return 0;
  100.         }
  101.     }
  102.     fclose(ipx_route);
  103.     return 1;
  104. }
  105.         
  106. int
  107. ipx_kern_scan_ifaces(IPXifcScanFunc f, void *data)
  108. {
  109.     FILE *ipx_ifc;
  110.     char buf[512];
  111.  
  112.     ipx_ifc = fopen("/proc/net/ipx_interface", "r");
  113.  
  114.     if (ipx_ifc == NULL)
  115.     {
  116.         sprintf(ipx_err_string, "open ipx_interface: %s",
  117.             strerror(errno));
  118.         return -1;
  119.     }
  120.  
  121.     /* ignore header line */
  122.     fgets(buf, sizeof(buf), ipx_ifc);
  123.  
  124.     while (fgets(buf, sizeof(buf), ipx_ifc) != NULL)
  125.     {
  126.         IPXNet network;
  127.         IPXNode node;
  128.         int type = 0;
  129.         char device[128];
  130.         int i;
  131.         int result;
  132.  
  133.         type = asc2frame(&(buf[46]));
  134.         
  135.         if (strncmp(&(buf[35]), "Internal", 8) == 0)
  136.         {
  137.             type |= IPX_KRT_INTERNAL;
  138.         }
  139.  
  140.         if (   (ipx_sscanf_net(buf, &network) != 0)
  141.             || (ipx_sscanf_node(&(buf[11]), node) != 0))
  142.         {
  143.             fclose(ipx_ifc);
  144.             return -1;
  145.         }
  146.  
  147.         memset(device, 0, sizeof(device));
  148.  
  149.         for (i = 0; i < sizeof(device)-1; i++)
  150.         {
  151.             if (buf[i+35] == ' ')
  152.             {
  153.                 break;
  154.             }
  155.             device[i] = buf[i+35];
  156.         }
  157.  
  158.         if ((result = f(network, node, device, type, data)) != 0)
  159.         {
  160.             fclose(ipx_ifc);
  161.             return result;
  162.         }
  163.     }
  164.     fclose(ipx_ifc);
  165.     return 0;
  166. }
  167.  
  168. int
  169. ipx_kern_route_add(int sock, IPXNet net, IPXNet rt_net, IPXNode rt_node)
  170. {
  171.     struct rtentry rt;
  172.     struct sockaddr_ipx* sr;
  173.     struct sockaddr_ipx* st;
  174.     
  175.     sr=(struct sockaddr_ipx*)&rt.rt_gateway;
  176.     st=(struct sockaddr_ipx*)&rt.rt_dst;
  177.     
  178.     sr->sipx_family=st->sipx_family=AF_IPX;
  179.     st->sipx_network=htonl(net);
  180.     sr->sipx_network=htonl(rt_net);
  181.     ipx_assign_node(sr->sipx_node,rt_node);
  182.     rt.rt_flags=RTF_GATEWAY;
  183.  
  184.     if(ioctl(sock,SIOCADDRT,(void *)&rt)!=0)
  185.     {
  186.         sprintf(ipx_err_string,"ioctl SIOCADDRT: %s",strerror(errno));
  187.         return -1;
  188.     }
  189.     return 0;
  190. }
  191.  
  192. int
  193. ipx_kern_route_delete(int sock, IPXNet net)
  194. {
  195.     struct rtentry rt;
  196.     struct sockaddr_ipx* sr;
  197.     struct sockaddr_ipx* st;
  198.     
  199.     sr=(struct sockaddr_ipx*)&rt.rt_gateway;
  200.     st=(struct sockaddr_ipx*)&rt.rt_dst;
  201.     
  202.     sr->sipx_family=st->sipx_family=AF_IPX;
  203.     st->sipx_network=htonl(net);
  204.     rt.rt_flags=RTF_GATEWAY;
  205.  
  206.     if(ioctl(sock,SIOCDELRT,(void *)&rt)!=0)
  207.     {
  208.         sprintf(ipx_err_string,"ioctl SIOCDELRT: %s",strerror(errno));
  209.         return -1;
  210.     }
  211.     return 0;
  212. }
  213.  
  214. int
  215. ipx_kern_enable_broadcast(int sock)
  216. {
  217.     int opt=1;
  218.     /* Permit broadcast output */
  219.     if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST, &opt,sizeof(opt))==-1)
  220.     {
  221.         sprintf(ipx_err_string,"setsockopt SO_BROADCAST: %s",
  222.             strerror(errno));
  223.         return -1;
  224.     }
  225.     return 0;
  226. }
  227.